package com.allin.mall.common.core.configurer;

import com.allin.mall.common.core.json.GsonDecoder;
import com.allin.mall.common.core.json.GsonEncoder;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.config.WebFluxConfigurer;

public class MallWebFluxConfigurer implements WebFluxConfigurer {

    @Resource
    private Gson gson;

    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {

        // 替换默认的解析器为gson
        configurer.registerDefaults(false);

        configurer.customCodecs().register(new EncoderHttpMessageWriter<Object>(new GsonEncoder(gson)));
        configurer.customCodecs().register(new DecoderHttpMessageReader<Object>(new GsonDecoder(gson)));
    }
}
